/* 
 * File:   ssvapi.cpp
 * Author: hanhnv
 * 
 * Created on 9 avril 2013, 21:44
 */

#include "ssvapi.h"

using namespace std;

ssvapi::ssvapi() {
    pathDLL = NULL;
    pathSesam = NULL;
    nomRessource = NULL;
    nomLecteur = NULL;
    codeAccess = NULL;
    dateConsultation = NULL;
}

ssvapi::ssvapi( const char* ressource,  const char* lecteur, const char* code,  const char* date,  const char* pDLL,  const char* pSesam) {
    pathDLL = (char*)pDLL;
    pathSesam = (char*)pSesam;
    nomRessource = (char*)ressource;
    nomLecteur = (char*)lecteur;
    codeAccess = (char*)code;
    dateConsultation = (char*)date;    
}

void ssvapi::setData( const char* ressource,  const char* lecteur,  const char* code,  const char* date,  const char* pDLL,  const char* pSesam) {
    pathDLL = (char*)pDLL;
    pathSesam = (char*)pSesam;
    nomRessource = (char*)ressource;
    nomLecteur = (char*)lecteur;
    codeAccess = (char*)code;
    dateConsultation = (char*)date;    
}

ssvapi::~ssvapi() {
}

void ssvapi::setCodeAccess(char* code)
{
    codeAccess = code;
}

void ssvapi::setDateConsultation(char* date)
{
    dateConsultation = date;
}

void ssvapi::setNomRessource(char* ressource)
{
    nomRessource = ressource;
}

void ssvapi::setNomeLecteur(char* lecteur)
{
    nomLecteur = lecteur;
}

void ssvapi::setPathDLL(char* path)
{
    pathDLL = path;
}

void ssvapi::setPathSesam(char* path)
{
    pathSesam = path;
}

string ssvapi::LireCartePS()
{
    string output = "";
    
    HINSTANCE LoadGIE;
    LoadGIE = LoadLibrary(pathDLL);
    
    SSV_LireCartePS ssv_LireCartePS = 0;    
    SSV_InitLIB2 ssv_InitLib = 0;
    
    ssv_InitLib = (SSV_InitLIB2)GetProcAddress(LoadGIE, "SSV_InitLIB2");
    
    if (ssv_InitLib)
    {
        const char* szSesamPath = pathSesam;
        unsigned short usLib;
        usLib = ssv_InitLib(szSesamPath);
        
        // Lire Carte PS
        ssv_LireCartePS = (SSV_LireCartePS)GetProcAddress(LoadGIE, "SSV_LireCartePS");
        if (ssv_LireCartePS)
        {       
            void* pBufferOutPS =  NULL;
            unsigned int pSize = 0;
            unsigned short usLirePS = ssv_LireCartePS(nomRessource, nomLecteur, codeAccess, &pBufferOutPS, &pSize);        
            char* pBuffer = (char*)pBufferOutPS;
            for (int i = 0; i < pSize; i++)    
                output = output + pBuffer[i];           
        }
    }    
    FreeLibrary(LoadGIE);

    return output;    
}

string ssvapi::LireDroitsVitale()
{ 
    string output = "";
    
    HINSTANCE LoadGIE;
    LoadGIE = LoadLibrary(pathDLL);
    
    SSV_LireDroitsVitale ssv_LireDroitsVitale = 0;    
    SSV_InitLIB2 ssv_InitLib = 0;
    
    ssv_InitLib = (SSV_InitLIB2)GetProcAddress(LoadGIE, "SSV_InitLIB2");
    
    if (ssv_InitLib)
    {
        const char* szSesamPath = pathSesam;
        unsigned short usLib;
        usLib = ssv_InitLib(szSesamPath);
        
        // Lire Carte PS
        ssv_LireDroitsVitale = (SSV_LireDroitsVitale)GetProcAddress(LoadGIE, "SSV_LireDroitsVitale");
        if (ssv_LireDroitsVitale)
        {       
            void* pBufferOutVitale = NULL;
            unsigned int pSize = 0;
            unsigned short usLireVitale = ssv_LireDroitsVitale(nomRessource, nomLecteur, codeAccess, dateConsultation, &pBufferOutVitale, &pSize);        
            char* pBuffer = (char*)pBufferOutVitale;
            for (int i = 0; i < pSize; i++)    
                output = output + pBuffer[i];           
        }
    }    
    FreeLibrary(LoadGIE);

    return output;    
}